package eu.telecombretagne.rsm.gui.frames;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JInternalFrame;

import eu.telecombretagne.rsm.gui.NameConsts;
import eu.telecombretagne.rsm.gui.frames.panels.ChoiceSettingsPanel;
import eu.telecombretagne.rsm.gui.frames.panels.ControlButtonsPanel;
import eu.telecombretagne.rsm.gui.frames.panels.DetailedSettingsPanel;
import eu.telecombretagne.rsm.gui.frames.panels.GlobalSettingsPanel;
import eu.telecombretagne.rsm.network.Network;
import eu.telecombretagne.rsm.protocol.test.Emetteur;
import eu.telecombretagne.rsm.protocol.test.Recepteur;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.*;


public class NetworkInternalFrame extends JInternalFrame implements ActionListener {
	private static final long serialVersionUID = 4497109065256440941L;
	
	static final int xOffset = 80, yOffset = 80;
	
	//panels
	DetailedSettingsPanel detailedSettingsPanel;
	GlobalSettingsPanel globalSettingsPanel;
	ChoiceSettingsPanel choiceSettingsPanel;
	ControlButtonsPanel controlButtonsPanel;
	
	//threads
	private Network network;
	private Emetteur emitter;
	private Recepteur receiver;
	
	/**
	 * The constructor of the NetworkInternalFrame.
	 */
	public NetworkInternalFrame(){
		super(NameConsts.NETWORKCONTROL,
				true,	//resizable
				true,	//closable
				true,	//maximizable
				true);	//iconifiable
		
		setSize(380,350);
		setFrameIcon(new ImageIcon(this.getClass().getResource(NameConsts.GFXCONTROL)));
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		setLocation(screenSize.width/2 - this.getWidth()/2, yOffset);
		
		setLayout(new BoxLayout(this.getContentPane(), BoxLayout.PAGE_AXIS));
		
		//settings panels
		createSettingsPanels();
		
		//settings selection
		createSettingsSelectionPanel();
		
		//control buttons
		createControlButtons();
	}
	
	private void createControlButtons(){
		controlButtonsPanel = new ControlButtonsPanel(this);
		this.getContentPane().add(controlButtonsPanel);
	}
	
	private void createSettingsSelectionPanel(){
		choiceSettingsPanel = new ChoiceSettingsPanel(this);
		this.getContentPane().add(choiceSettingsPanel);
	}
	
	private void createSettingsPanels(){
		globalSettingsPanel = new GlobalSettingsPanel();
		detailedSettingsPanel = new DetailedSettingsPanel();
		
		this.getContentPane().add(globalSettingsPanel);
		this.getContentPane().add(detailedSettingsPanel);
	}
	
	private void readAllProbabilities(){
		if(choiceSettingsPanel.isGlobalSettingsSelected()){
			globalSettingsPanel.readAllParams();
		} else {
			detailedSettingsPanel.readAllParams();
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent actionEvent){
		if("rdBtnSimplified".equals(actionEvent.getActionCommand())){
			detailedSettingsPanel.setPanelLocked(true);
			globalSettingsPanel.setPanelLocked(false);
			
		} else if("rdBtnDetailed".equals(actionEvent.getActionCommand())){
			globalSettingsPanel.setPanelLocked(true);
			detailedSettingsPanel.setPanelLocked(false);
			
		} else if("btnStartNetwork".equals(actionEvent.getActionCommand())){
			if(network == null){
				readAllProbabilities();
				network = (new Network()).start();
			}
			
			choiceSettingsPanel.setPanelLocked(true);
			controlButtonsPanel.setButtonStartNetworkEnabled(false);
			controlButtonsPanel.setButtonStartTransmissionEnabled(true);
			
		} else if("btnStartTransmission".equals(actionEvent.getActionCommand())){
			if(receiver == null){
				receiver = new Recepteur();
			}
				
			if(emitter == null){
				emitter = new Emetteur();
			}
			
			controlButtonsPanel.setButtonStopTransmissionEnabled(true);
			
		} else if("btnStopTransmission".equals(actionEvent.getActionCommand())){
			emitter.stopEmetteur();
			emitter = null;
			receiver.stopRecepteur();
			receiver = null;
			network = network.stop();
			
			choiceSettingsPanel.setPanelLocked(false);
			controlButtonsPanel.setButtonStartNetworkEnabled(true);
			controlButtonsPanel.setButtonStartTransmissionEnabled(false);
		}
	}
}